package com.kele.controller;

import com.kele.api.goods.PageControllerApi;
import com.kele.client.GoodsFeignClient;
import com.kele.common.model.response.PageResult;
import com.kele.common.model.response.Result;
import com.kele.model.goods.Goods;
import com.kele.model.goods.dto.GoodsDTO;
import com.kele.service.PageService;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("page")
public class PageController implements PageControllerApi {

    @Autowired
    PageService pageService;
    @Autowired
    GoodsFeignClient goodsFeignClient;

    @GetMapping("goods/selectById/{goodsId}")
    public GoodsDTO selectById(@PathVariable  Long goodsId){
        return  goodsFeignClient.selectById(goodsId);
    }

    @PostMapping("goods/findPage/{pageNo}/{pageSize}")
    public PageResult<Goods> findPage(@PathVariable Integer pageNo,
                                      @PathVariable Integer pageSize,
                                      @RequestBody Map searchMap){
        return  goodsFeignClient.findPage(pageNo,pageSize,searchMap);
    }

    @Override
    @GetMapping("genHtml/{goodsId}")
    public Result genHtml(@PathVariable Long goodsId) throws IOException {
        Result result = null;
        try {
            result = pageService.genHtml(goodsId);
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Autowired
    HttpServletResponse response;

    @GetMapping("genGoodsListHtml_makeForOrder")
    public Result genGoodsListHtml_makeForOrder(HttpServletResponse response) throws IOException {
        Result result = null;
        try {
            result = pageService.genGoodsListHtml_makeForOrder(response);
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * make for stock
     * @return
     * @throws IOException
     */
    @GetMapping("genGoodsListHtml_outOfBox")
    public Result genGoodsListHtml_outOfBox() throws IOException {
        Result result = null;
        try {
            result = pageService.genGoodsListHtml_outOfBox();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return result;
    }
    @GetMapping("genGoodsListHtml_assembly")
    public Result genGoodsListHtml_assembly() throws IOException {
        Result result = null;
        try {
            result = pageService.genGoodsListHtml_assembly();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("genGoodsListHtml_afterSale/{pageNo}/{pageSize}")
    public Result genGoodsListHtml_afterSale(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize
    ) throws IOException {
        Result result = null;
        try {
            result = pageService.genGoodsListHtml_afterSale(pageNo,pageSize);
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return result;
    }
}
